package com.javamodeling.android.locationpath;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MapDataActivity extends Activity {

	ListView listview = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		listview = (ListView)findViewById(android.R.id.list);
		
		Button btnToday = (Button)findViewById(R.id.btnDataToday);
		btnToday.setOnClickListener(btnTodayClickListener);
		
		Button btnYesterday = (Button)findViewById(R.id.btnDataYesterday);
		btnYesterday.setOnClickListener(btnYesterdayClickListener);

		Button btnHalfDay = (Button)findViewById(R.id.btnDataHalfDay);
		btnHalfDay.setOnClickListener(btnHalfDayClickListener);
		
		Button btnAllDay = (Button)findViewById(R.id.btnDataAll);
		btnAllDay.setOnClickListener(btnAllDayClickListener);
	}

	private OnClickListener btnTodayClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date(System.currentTimeMillis());
			String currentTime = simple.format(date).toString();
			
			MapDBAdapter mapDBAdapter = MapDBAdapter.getInstance(MapDataActivity.this);
			
			Cursor cursor = mapDBAdapter.searchByStartEnd(currentTime + " 00:00:00", currentTime + " 99:99:99");
			
			showData(cursor);
		}
	};
	
	private OnClickListener btnYesterdayClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date(System.currentTimeMillis());
			String src = simple.format(date).toString();
			
			GregorianCalendar cal = new GregorianCalendar();
	        try {
	            cal.setTime(simple.parse(src));
	        } catch (ParseException ex) {
	        }
	        cal.add(GregorianCalendar.DATE, -1);
	        String currentTime = simple.format(cal.getTime()).toString();
			
			MapDBAdapter mapDBAdapter = MapDBAdapter.getInstance(MapDataActivity.this);
			
			Cursor cursor = mapDBAdapter.searchByStartEnd(currentTime + " 00:00:00", currentTime + " 99:99:99");
			
			showData(cursor);
			
		}
	};
	
	private OnClickListener btnHalfDayClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH");
			Date date = new Date(System.currentTimeMillis());
			String src = simple.format(date).toString();
			
			GregorianCalendar cal = new GregorianCalendar();
	        try {
	            cal.setTime(simple.parse(src));
	        } catch (ParseException ex) {
	        }
	        cal.add(GregorianCalendar.HOUR, -12);
	        String currentTime = simple.format(cal.getTime()).toString();
	        
			MapDBAdapter mapDBAdapter = MapDBAdapter.getInstance(MapDataActivity.this);
			
			Cursor cursor = mapDBAdapter.searchByStartEnd(currentTime + ":00:00", src + ":99:99");
			
			showData(cursor);
			
		}
	};

	private OnClickListener btnAllDayClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			MapDBAdapter mapDBAdapter = MapDBAdapter.getInstance(MapDataActivity.this);
			
			Cursor cursor = mapDBAdapter.searchAll();
			
			showData(cursor);
			
		}
	};
	
	private void showData(Cursor cursor) {
		
		startManagingCursor(cursor);
		
		String[] from = {"latitude", "longitude", "input_date"};
		int[] to = {R.id.txtLatitude, R.id.txtLongitude, R.id.txtInputDate};
		
		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(MapDataActivity.this, R.layout.list_item, cursor, from, to);
		
		listview.setAdapter(simpleCursorAdapter);

	}
	
//	public void listSearch(View view) {
//
//		String[] from = {"time"};
//		int[] to = {R.id.txtLatitude};
//
//		SimpleAdapter simpleAdapter = new SimpleAdapter(this, LocationPathListener.mapLists, R.layout.list_item, from, to);
//		
//		listview.setAdapter(simpleAdapter);
//		
//	}
}
